package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1711 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 出题人，我日你妈
     * @return:
     */
    class Solution {

        private int factor = 1000000007;

        public int countPairs(int[] deliciousness) {
            final Set<Integer> twoSet = generateTwoSet();
            TreeMap<Integer, Long> cnt = new TreeMap<>();
            for (int num : deliciousness) {
                cnt.put(num, cnt.getOrDefault(num, 0L) + 1);
            }

            int res = 0;
            for (Integer num : cnt.keySet()) {
                for (Integer twoNum : twoSet) {
                    int other = twoNum - num;
                    if (cnt.containsKey(other) && other >= num) {
                        if (num == other) res = (int) ((res + cnt.get(num) * (cnt.get(num) - 1) / 2) % factor);
                        else res = (int) ((res + cnt.get(num) * cnt.get(other)) % factor);
                    }
                }
            }

            return res;
        }

        public Set<Integer> generateTwoSet() {
            Set<Integer> twoSet = new HashSet<>();
            int curNum = 1;
            for (int i = 1; i <= 23; i++) {
                twoSet.add(curNum);
                curNum *= 2;
            }

            return twoSet;
        }
    }

    public void run() {
        Solution solution = new Solution();
//        int[] arr = InputUtil.toIntegerArray("[1048576,1048576]");//2,097,152
        int[] arr = new int[100000];
        for (int i = 0; i < 100000; i++) {
            arr[i] = 32;
        }
        System.out.println(solution.countPairs(arr));

        int num = 100000;
        final long lt = num * (num - 1) / 2;
        long res = 0;
        System.out.println((0L + num * (num - 1) / 2) % 1000000007);
        final long al = lt % 1000000007;
        System.out.println(al);
    }

    public static void main(String[] args) throws Exception {
        LC1711 an = new LC1711();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
